#include "KeyRecommander.h"
#include "nlohmann/json.hpp"

using Json = nlohmann::json;

void KeyRecommander::doQuery(const string& word,string& result){
    //清空prique
    priority_queue<CandidateResult> empty;
    _prique.swap(empty);
    //得到未排序的CandidateResult的列表
    vector<CandidateResult> reslist;
    _dict->doQuery(word,reslist);
    //将列表中的内容写入prique中进行排序
    for(CandidateResult elem : reslist){
        _prique.push(elem);
            /* cout << "elem._word = " << elem._word << endl; */
            /* cout << "elem._dist = " << elem._dist << endl; */
            /* cout << "elem._freq = " << elem._freq << endl; */
    }
    Json Result;
    //从prique中选出前十个作为结果
    for(int idx = 0; idx < 10; ++idx){
        if(!_prique.empty()){
            const CandidateResult& res = _prique.top();
            //转换成Json形式
            string key = "key" + std::to_string(idx);
            Result[key] = res._word;
            /* result = result + res._word + " "; */
            /* cout << "res._word = " << res._word << endl; */
            /* cout << "res._dist = " << res._dist << endl; */
            /* cout << "res._freq = " << res._freq << endl; */
            _prique.pop();
        }else{
            break;
        }
    }
    //返回Json类型的字符串
    result = Result.dump();
}
